package com.proactive.webapp.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.IOUtils;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;

public class AwsUtil {
	/*參考1 http://weblog4j.com/2013/03/13/developing-java-web-application-powered-by-amazon-s3/
	 *參考2 aws-java-sdk-1.9.13\samples\AmazonS3\S3Sample.java
	 * */
	private BasicAWSCredentials credentials;
    private AmazonS3 s3;
    private String bucketName;
    private String propertyPath;
    private static volatile AwsUtil awsUtil = new AwsUtil();
    public static String url = "https://s3-ap-northeast-1.amazonaws.com/proactive/";
    
    public AwsUtil getInstance(){
    	try{
            Properties properties = new Properties();
            properties.load(new FileInputStream(propertyPath));
            credentials = new BasicAWSCredentials(properties.getProperty("accessKey"),
                                                         properties.getProperty("secretKey"));
            bucketName = "proactive";
            s3 = new AmazonS3Client(this.credentials);

        }catch(Exception e){
            System.out.println("exception while creating awss3client : " + e);
        }
        return awsUtil;
    }
 
    public static AmazonS3 getAWSClient(){
         return awsUtil.s3;
    }
 
   
    
    //Upload an object to your bucket:
    public void uploadFile(String path, File file) throws FileNotFoundException{
        /**
         * key should be unique.
         * an whatever key you set will be used to in url path to access the pic.
         */
        String key = path + file.getName();
        s3.putObject(new PutObjectRequest(this.bucketName, key, file).withCannedAcl(CannedAccessControlList.PublicRead));
        //withCannedAcl(CannedAccessControlList.PublicRead) make public
    }
    
    //Download an object:(後來使用Properties裡Link網址來下載，此method只能取得object，並對其做其他操作設定比如設定metadata，但s3上並不會改變)
    public S3Object downloadFileFromS3(String bucket, String key) throws IOException{
        GetObjectRequest getObjectRequest = new GetObjectRequest(this.bucketName, key);
        S3Object s3object = s3.getObject(getObjectRequest);
      
		return s3object;    
    }
    
    //Delete an object:
    public void deleteFile(String key){
        s3.deleteObject(this.bucketName, key);
    }
    
    //List the buckets in your account:
    public List<Bucket> listBuckets(){
        for (Bucket bucket : s3.listBuckets()) {
                System.out.println("bucket name - " + bucket.getName());
        }
        return s3.listBuckets();
    }
    
    public S3Object getS3Object(String bucket, String key){     
    	S3Object s3object = s3.getObject(new GetObjectRequest(bucketName, key));
		return s3object;
    }

	public void setPropertyPath(String propertyPath) {
		this.propertyPath = propertyPath;
	}
    
    
}
